package edu.stanford.bmir.protege.web.server.transform;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntologyID;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import edu.stanford.bmir.protege.web.client.ui.ontology.transform.service.TransformService;
import edu.stanford.bmir.protege.web.server.filesubmission.FileUploadConstants;
import edu.stanford.bmir.protege.web.server.owlapi.OWLAPIProject;
import edu.stanford.bmir.protege.web.server.owlapi.OWLAPIProjectManager;
import edu.stanford.bmir.protege.web.server.transform.service.CreateOwlService;
import edu.stanford.bmir.protege.web.shared.project.ProjectId;
import edu.stanford.bmir.protege.web.shared.transform.MappingBean;

public class TransformServiceImpl  extends RemoteServiceServlet implements TransformService {
	
	private final File uploadsDirectory = FileUploadConstants.UPLOADS_DIRECTORY;
	

	@Override
	public List<MappingBean> getMappingList(String fileName) {
		
		if ( fileName.contains( "<" ) && fileName.contains( ">" ) ) {
			
			int index = fileName.indexOf( ">" );
			fileName = fileName.substring( index+1 );
			
		}
		
		String filePath = uploadsDirectory.getAbsolutePath()+File.separator + fileName;

		CreateOwlService cso = new CreateOwlService( filePath );
		List<MappingBean> mblst = cso.getProperties();
		
		return mblst;
	}

	public String createOWL(String fileName, ProjectId projectId,Map<String, String> pros)  {
		
		if ( fileName.contains( "<" ) && fileName.contains( ">" ) ) {
			
			int index = fileName.indexOf( ">" );
			fileName = fileName.substring( index+1 );
			
		}
		
		String filePath = uploadsDirectory.getAbsolutePath()+File.separator + fileName;
		
		OWLAPIProjectManager projectManager =  OWLAPIProjectManager.getProjectManager();
		
	    OWLAPIProject project = projectManager.getProject(projectId);
        OWLOntologyID id = project.getRootOntology().getOntologyID();
        
        IRI ontologyIRI = id.getOntologyIRI();
        
        CreateOwlService cso = new CreateOwlService( filePath,pros, ontologyIRI);
        String path = cso.getOWLPath();
        
		return path;
		
	}
	

}
